package httpHandler

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/lovesport-user-service/app/config"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model/req"
	"gitee.com/zaiqiang231/lovesport-user-service/app/store"
	"gitee.com/zaiqiang231/lovesport-user-service/app/util"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"net/http"
)

func PolicyCreate(c *gin.Context) {
	var policyReq req.PolicyCreateReq
	if err := c.ShouldBindJSON(&policyReq); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	validate := validator.New()
	util.ValidateDescription(validate)
	util.ValidateName(validate)
	if err := validate.Struct(policyReq); err != nil {
		baseLog.Errorf("validate policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	var createInfo model.Policy
	username := c.GetString(config.AuthIdentityKey)
	createInfo.Name = policyReq.Name
	createInfo.UserName = username
	createInfo.Policy = policyReq.Policy

	if err := store.GetDataStore().Policy().Create(&createInfo); err != nil {
		baseLog.Errorf("create policy fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	WriteResponse(c, 0, "create policy success", createInfo)
}

func PolicyList(c *gin.Context) {
	var opts store.PolicyListOptions
	if err := c.ShouldBindQuery(&opts); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	username := c.GetString(config.AuthIdentityKey)
	list, err := store.GetDataStore().Policy().List(opts, username)
	if err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	WriteResponse(c, 0, "List success", list)
}

func PolicyGet(c *gin.Context) {
	name := c.Param("name")
	username := c.GetString(config.AuthIdentityKey)
	policy, err := store.GetDataStore().Policy().Get(username, name)
	if err != nil {
		baseLog.Errorf("get policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	WriteResponse(c, 0, "get policy info success", policy)
}

func PolicyUpdate(c *gin.Context) {

	var updateInfo req.PolicyUpdateReq
	if err := c.ShouldBindJSON(&updateInfo); err != nil {
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	name := c.Param("name")
	username := c.GetString(config.AuthIdentityKey)
	target, err := store.GetDataStore().Policy().Get(username, name)
	if err != nil {
		baseLog.Errorf("get policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	target.Policy = updateInfo.Policy

	validate := validator.New()
	util.ValidateDescription(validate)
	util.ValidateName(validate)
	if err := validate.Struct(target); err != nil {
		baseLog.Errorf("validate policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	updateErr := store.GetDataStore().Policy().Update(target)
	if updateErr != nil {
		baseLog.Errorf("update user info fail, %v", updateErr)
		WriteResponse(c, http.StatusBadRequest, updateErr.Error(), nil)
		return
	}

	WriteResponse(c, 0, "update Policy info success", target)
}

func PolicySoftDelete(c *gin.Context) {
	name := c.Param("name")
	username := c.GetString(config.AuthIdentityKey)

	var err error
	_, err = store.GetDataStore().Policy().Get(username, name)
	if err != nil {
		baseLog.Errorf("get Policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	err = store.GetDataStore().Policy().Delete(username, name, true)
	if err != nil {
		baseLog.Errorf("get Policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	WriteResponse(c, 0, "delete Policy info success", nil)
}

func PolicyDelete(c *gin.Context) {
	name := c.Param("name")
	username := c.GetString(config.AuthIdentityKey)

	var err error
	_, err = store.GetDataStore().Policy().Get(username, name)
	if err != nil {
		baseLog.Errorf("get Policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	err = store.GetDataStore().Policy().Delete(username, name, false)
	if err != nil {
		baseLog.Errorf("get Policy info fail, %v", err)
		WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	WriteResponse(c, 0, "delete Policy info success", nil)
}
